import Vue from 'vue'
import Vuex from 'vuex'
import VueXAlong from 'vuex-along'
import { formatRouterTree, generateRouter} from '../utils/RouterTree'
import { getUserRouters } from '../utils/api'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    userinfo: {},
    userRouters: [],
    isAddRouters:false,
    token: ""
  },
  getters: {

  },
  mutations: {
    // 存储后台获取的路由数组
    setuserRouters (state, userRouters) {
      state.userRouters = userRouters;
    },
    // 改变权限状态
    setAddRouters(state){
      state.isAddRouters = true;
    }
  },
  actions: {
    // 将获取的动态路由格式化成树结构
    async setUserRouters ({commit, state}) {
      const params = new URLSearchParams();
      params.append("uid",state.userinfo.tno);
      const userRouters = await getUserRouters(params);
      const payload = formatRouterTree(userRouters.data);
      commit('setuserRouters', payload);
    },
    setAddRouters({commit}){
      commit('setAddRouters');
    }
  },
  modules: {
  },
  plugins: [VueXAlong({
    name: 'along',     //存放在localStroage或者sessionStroage 中的名字
    local: {list: ['token']},      //是否存放在local中  false 不存放 如果存放按照下面session的配置配
    session: { list: [''] , isFilter:true} //如果值不为false 那么可以传递对象 其中 当isFilter设置为true时， list 数组中的值就会被过滤调,这些值不会存放在seesion或者local中
  })]
})
